package com.stu.boke.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.stu.boke.entity.Tag;
import com.stu.boke.service.TagService;

@Controller
@RequestMapping(value = "/admin/tag")
public class TagController {

	@Autowired
	private TagService tagService;
	
	@RequestMapping(value = "")
	public ModelAndView indexCategory() {
		ModelAndView model = new ModelAndView();
		List<Tag> tagList = tagService.listTagWithCount();
		model.addObject("tagList", tagList);
		model.setViewName("/Admin/Tag/index");
		return model;
	}
	
	@RequestMapping(value = "/insertSubmit")
	public String insertSubmitTag(Tag tag) {
		tagService.insertSubmitTag(tag);
		return "redirect:/admin/tag";
	}
	
	@RequestMapping(value = "/edit/{id}")
	public ModelAndView editTagView(@PathVariable("id") Integer id) {
		ModelAndView modelAndView = new ModelAndView();
		Tag tag = tagService.getTagById(id);
		modelAndView.addObject("tag", tag);
		List<Tag> tagWithCount = tagService.listTagWithCount();
		modelAndView.addObject("tagList", tagWithCount);
		modelAndView.setViewName("Admin/Tag/edit");
		return modelAndView;
	}
	
	@RequestMapping(value = "/editSubmit")
	public String editTagSubmit(Tag tag) {
		tagService.updateTag(tag);
		return "redirect:/admin/tag";
	}
	
	@RequestMapping(value = "/delete/{id}")
	public String deleteTag(@PathVariable("id") Integer id) {
		tagService.deleteTag(id);
		return "redirect:/admin/tag";
	}
}
